package com.easylinkin.linkappapi.lobar.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

/**
 * @author xiaoyang
 * @version 1.0
 * @date 2022/11/23
 * @description 阿里云全国天气预报查询api
 */
@Slf4j
@Component
public class AlicloudWeatherApi {

  // 四个直辖市
  private static final List<String> proviceCity = Arrays.asList("北京市", "天津市", "上海市", "重庆市");

  public static void main(String[] args) {
//    String area = projectAreaToweaterArea("河南省,焦作市,沁阳市");
//    System.out.println(area);
//    queryWeather(area);
    queryWeatherFromLocation("30.488072", "114.158784");
  }

  /**
   * 经纬度查天气
   *
   * @param lat 纬度值
   * @param lng 经度值
   */
  public static JSONObject queryWeatherFromLocation(String lat, String lng) {
    if (StringUtils.isEmpty(lat) || StringUtils.isEmpty(lng)) {
      return null;
    }
    String host = "https://qryweather.market.alicloudapi.com";
    String path = "/lundroid/queryweatherfromlocation";
    String method = "GET";
    String appcode = "437e47c104d84e7ab333cf323c9c1ad6";
    Map<String, String> headers = new HashMap<>();
    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
    headers.put("Authorization", "APPCODE " + appcode);
    Map<String, String> querys = new HashMap<>();
    querys.put("lat", lat);
    querys.put("lng", lng);

    try {
      log.info("AlicloudWeatherApi queryWeatherFromLocation req:{}", querys);
      HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
      //获取response的body
      JSONObject result = JSON.parseObject(EntityUtils.toString(response.getEntity()));
      log.info("AlicloudWeatherApi queryWeatherFromLocation res:{}", result);
      if (null != result) {
        JSONObject resp = result.getJSONObject("resp");
        String respMsg = resp.getString("RespMsg");
        Integer respCode = resp.getInteger("RespCode");
        if (respMsg.equals("ok")) {
          return result.getJSONObject("data");
        } else {
          log.error("AlicloudWeatherApi queryWeatherFromLocation RespMsg:{},RespCode:{}", respMsg,
              respCode);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 地名查询天气
   */
  public static JSONObject queryWeather(String area) {
    if (StringUtils.isEmpty(area)) {
      return null;
    }
    String host = "https://qryweather.market.alicloudapi.com";
    String path = "/lundroid/queryweather";
    String method = "GET";
    String appcode = "437e47c104d84e7ab333cf323c9c1ad6";
    Map<String, String> headers = new HashMap<>();
    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
    headers.put("Authorization", "APPCODE " + appcode);
    Map<String, String> querys = new HashMap<>();
    querys.put("area", area);
//    querys.put("areaid", "101200101");

    try {
      log.info("AlicloudWeatherApi queryWeather req:{}", querys);
      HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
      //获取response的body
      JSONObject result = JSON.parseObject(EntityUtils.toString(response.getEntity()));
      log.info("AlicloudWeatherApi queryWeather res:{}", result);

      if (null != result) {
        JSONObject resp = result.getJSONObject("resp");
        String respMsg = resp.getString("RespMsg");
        Integer respCode = resp.getInteger("RespCode");
        if (respMsg.equals("ok")) {
          log.info("AlicloudWeatherApi queryWeather data:{}", result.getJSONObject("data"));
          //获取response的body
          return result.getJSONObject("data");
        } else {
          log.error("AlicloudWeatherApi queryWeather RespMsg:{},RespCode:{}", respMsg, respCode);
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

  /**
   * 项目地区转换天气地区
   */
  public static String projectAreaToweaterArea(String projectArea) {
    String defaultArea = "武汉";
    if (StringUtils.isEmpty(projectArea)) {
      return defaultArea;
    }
    String[] areas = projectArea.split(",");
    if (areas.length < 3) {
      return defaultArea;
    }
    String province = areas[0];
    String city = areas[1];
    if (proviceCity.contains(province)) {
      city = areas[0];
    }
    return city.substring(0, city.length() - 1);
  }
}
